package btc.backend.process;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionFactory {

    public static String DB_NAME="btc_db";

    private static Connection conexion = null;
    
	public static Connection getConnection(){
		
		if (conexion == null){
				
			String driver = "org.apache.derby.jdbc.EmbeddedDriver";
				
			try {
				Class.forName(driver);
			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			}
	
	        Properties prop = new Properties();
	        InputStream inputStream = ConnectionFactory.class.getClassLoader().getResourceAsStream("derby.properties");
	        try {
				prop.load(inputStream);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
	        String location = prop.getProperty("derby.location");
	
	        String connectionURL = "jdbc:derby:" + location + File.separator + DB_NAME + ";create=true";
			try {
				conexion = DriverManager.getConnection(connectionURL); 
				return conexion;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	
		return conexion;
	}

}
